try{	
	customerPieData = customerPieData && customerPieData.length > 0 ? genPieData(customerPieData) : customerPieData;
	
	var customerPieOption = {
		    title : {
		        text: '按户号统计故障记录',
		        subtext: '数量统计',
		        x:'center'
		    },
		    toolbox: {
		        show: true,
		        feature: {
		            dataView: {readOnly: false},
		            saveAsImage: {}
		        },
		        zlevel: 1,
		        orient: 'vertical',
		        itemGap: 20,
		        left: 20
		    },
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		        type: 'scroll',
		        orient: 'vertical',
		        right: 10,
		        top: 20,
		        bottom: 20,
		        data: customerPieData.legendData
		    },
		    series : [
		        {
		            name: '户号',
		            type: 'pie',
		            radius : '70%',
		           
		            data: customerPieData.seriesData,
		            label: {
		                normal: {
		                    show: false,
		                    position: 'center'
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: false
		                }
		            }
		        }
		    ]
		};
} catch(e){
	console.error(e);
	console.error("生成户号饼图时遇到错误");
}

try{
	repairTypePieData = repairTypePieData && repairTypePieData.length > 0 ? genPieData(repairTypePieData) : repairTypePieData;

	var repairTypePieOption = {
	    title : {
	        text: '按故障类型统计故障记录',
	        subtext: '数量统计',
	        x:'center'
	    },
	    toolbox: {
	        show: true,
	        feature: {
	            dataView: {readOnly: false},
	            saveAsImage: {}
	        },
	        zlevel: 1,
	        orient: 'vertical',
	        itemGap: 20,
	        left: 20
	    },
	    tooltip : {
	        trigger: 'item',
	        formatter: "{a} <br/>{b} : {c} ({d}%)"
	    },
	    legend: {
	        type: 'scroll',
	        orient: 'vertical',
	        right: 10,
	        top: 20,
	        bottom: 20,
	        data: repairTypePieData.legendData
	    },
	    series : [
	        {
	            name: '故障类型',
	            type: 'pie',
	            radius : '70%',
	           
	            data: repairTypePieData.seriesData,
	            label: {
	                normal: {
	                    show: false,
	                    position: 'center'
	                }
	            },
	            labelLine: {
	                normal: {
	                    show: false
	                }
	            }
	        }
	    ]
	};
} catch(e){
	console.error(e);
	console.error("生成故障类型饼图时遇到错误");
}

try{
	voltageLevelPieData = voltageLevelPieData && voltageLevelPieData.length > 0 ? genPieData(voltageLevelPieData) : voltageLevelPieData;

	var voltageLevelPieOption = {
	    title : {
	        text: '按电压等级统计故障记录',
	        subtext: '数量统计',
	        x:'center'
	    },
	    toolbox: {
	        show: true,
	        feature: {
	            dataView: {readOnly: false},
	            saveAsImage: {}
	        },
	        zlevel: 1,
	        orient: 'vertical',
	        itemGap: 20,
	        left: 20
	    },
	    tooltip : {
	        trigger: 'item',
	        formatter: "{a} <br/>{b} : {c} ({d}%)"
	    },
	    legend: {
	        type: 'scroll',
	        orient: 'vertical',
	        right: 10,
	        top: 20,
	        bottom: 20,
	        data: voltageLevelPieData.legendData
	    },
	    series : [
	        {
	            name: '电压等级',
	            type: 'pie',
	            radius : '70%',
	           
	            data: voltageLevelPieData.seriesData,
	            label: {
	                normal: {
	                    show: false,
	                    position: 'center'
	                }
	            },
	            labelLine: {
	                normal: {
	                    show: false
	                }
	            }
	        }
	    ]
	};
} catch(e){
	console.error(e);
	console.error("生成电压等级饼图时遇到错误");
}

try{
	repairTypeLineData = repairTypeLineData && repairTypeLineData.length > 0 ? genLineData(repairTypeLineData,allRepairType,"repairType") : repairTypeLineData;
	var repairTypeLineOption = {
		    title: {
		        text: '按维修日期、故障类型统计故障记录',
		        subtext: '数量统计'
		    },
		    tooltip: {
		        trigger: 'axis'
		    },
		    legend: {
		    	type: 'scroll',
		        top: 0,
		        data: repairTypeLineData.legendData
		    },
		    grid: {
		        left: '3%',
		        right: '4%',
		        bottom: '40',
		        containLabel: true
		    },
		    toolbox: {
		        feature: {
		            magicType: {
		                type: ['line', 'bar']
		            },
		            dataZoom: {
		                yAxisIndex: 'none'
		            },
		            restore: {},
		            saveAsImage: {}
		        }
		    },
		    dataZoom: [
		        {
		            show: true,
		            realtime: true,
		            start: 0,
		            end: 100
		        }
		    ],
		    xAxis: {
		        type: 'category',
		        boundaryGap: false,
		        data: repairTypeLineData.xData
		    },
		    yAxis: {
		        type: 'value'
		    },
		    series: repairTypeLineData.seriesData
		};
} catch(e){
	console.error(e);
	console.error("生成故障类型折线图时遇到错误");
}

try{
	voltageLevelLineData = voltageLevelLineData && voltageLevelLineData.length > 0 ? genLineData(voltageLevelLineData,allVoltageLevel,"value") : voltageLevelLineData;

	var voltageLevelLineOption = {
		    title: {
		        text: '按维修日期、电压等级统计故障记录',
		        subtext: '数量统计'
		    },
		    tooltip: {
		        trigger: 'axis'
		    },
		    legend: {
		    	type: 'scroll',
		        top: 0,
		        data: voltageLevelLineData.legendData
		    },
		    grid: {
		        left: '3%',
		        right: '4%',
		        bottom: '40',
		        containLabel: true
		    },
		    toolbox: {
		        feature: {
		            magicType: {
		                type: ['line', 'bar']
		            },
		            dataZoom: {
		                yAxisIndex: 'none'
		            },
		            restore: {},
		            saveAsImage: {}
		        }
		    },
		    dataZoom: [
		        {
		            show: true,
		            realtime: true,
		            start: 0,
		            end: 100
		        }
		    ],
		    xAxis: {
		        type: 'category',
		        boundaryGap: false,
		        data: voltageLevelLineData.xData
		    },
		    yAxis: {
		        type: 'value'
		    },
		    series: voltageLevelLineData.seriesData
		};
} catch(e){
	console.error(e);
	console.error("生成电压等级折线图时遇到错误");
}

try{
	customerVoltageLossData = customerVoltageLossData && customerVoltageLossData.length > 0 ? genCustomerVoltageLoss(customerVoltageLossData,allVoltageLevel) : customerVoltageLossData;
	
	var customerVoltageLossOption = {
		    title: {
		        text: '按维修户号、电压等级统计失压故障记录',
		        subtext: '数量统计'
		    },
		    tooltip: {
		        trigger: 'axis'
		    },
		    legend: {
		    	type: 'scroll',
		        top: 0,
		        data: customerVoltageLossData.legendData
		    },
		    grid: {
		        left: '3%',
		        right: '4%',
		        bottom: '40',
		        containLabel: true
		    },
		    toolbox: {
		        feature: {
		            dataZoom: {
		                yAxisIndex: 'none'
		            },
		            restore: {},
		            saveAsImage: {}
		        }
		    },
		    dataZoom: [
		        {
		            show: true,
		            realtime: true,
		            start: 0,
		            end: 100
		        }
		    ],
		    xAxis: {
		        type: 'category',
		        boundaryGap: true,
		        data: customerVoltageLossData.xData
		    },
		    yAxis: {
		        type: 'value'
		    },
		    series: customerVoltageLossData.seriesData
		};
} catch(e){
	console.error(e);
	console.error("生成户号、电压等级折线图时遇到错误");
}
	
	function genPieData(data) {
		var legendData = []
		var seriesData = [];
		
		for(var i in data){
			legendData.push(data[i]['name']);
			seriesData.push({name: data[i]['name'],value: data[i]['counts']});
		}
		
	    return {
	        legendData: legendData,
	        seriesData: seriesData
	    };
	}
	
	function genLineData(data,keyArray,keyName) {
		var legendData = []
		var seriesData = [];
		
		var keyObject = {};
		
		for(var i = 0;i < keyArray.length;i++){
			keyObject[keyArray[i]["seqId"]] = {};
			keyObject[keyArray[i]["seqId"]]["name"] = keyArray[i][keyName];
			keyObject[keyArray[i]["seqId"]]["nameIndex"] = i;
			keyObject[keyArray[i]["seqId"]]["dataIndex"] = 0;
			legendData.push(keyArray[i][keyName] + "");
		}
		
		var startDate = $("#startDate input[name='startDate']").val().substring(0,data[0]['days'].length);
		var endDate = $("#endDate input[name='endDate']").val().substring(0,data[data.length - 1]['days'].length);
		
		var dateArray = [];
		
		var minDays = data[0]['days'] > startDate && startDate ? startDate : data[0]['days'];
		var minDaysArray = minDays.split("-");
		for(var i in minDaysArray){
			if(!isNaN(parseInt(i))){
				minDaysArray[i] = parseInt(minDaysArray[i].replace(/^[0]+/,""));
			}
		}
		if(minDaysArray[1]){
			minDaysArray[1]--;
		}
		var minDate = toDate(minDays);
		var maxDays = data[data.length - 1]['days'] < endDate && endDate ? endDate : data[data.length - 1]['days'];
		var maxDaysArray = maxDays.split("-");
		for(var i in maxDaysArray){
			if(!isNaN(parseInt(i))){
				maxDaysArray[i] = parseInt(maxDaysArray[i].replace(/^[0]+/,""));
			}
		}
		if(maxDaysArray[1]){
			maxDaysArray[1]--;
		}
		var maxDate = toDate(maxDays);

		if(minDays <= maxDays && minDaysArray.length > 0 && maxDaysArray.length == minDaysArray.length){
			var tempDate;
			dateArray.push(minDays);

			while((tempDate = (new Date(minDaysArray.length == 1 ? ++minDaysArray[0] : minDaysArray[0],minDaysArray.length == 2 ? ++minDaysArray[1] : (minDaysArray[1] ? minDaysArray[1] : 0),minDaysArray.length == 3 ? ++minDaysArray[2] : (minDaysArray[2] ? minDaysArray[2] : 1)))) <= maxDate){
				var tempStr = tempDate[dateFunctions[0]]();
				for(var j = 1;j < minDaysArray.length;j++){
					var tempValue = tempDate[dateFunctions[j]]();
					if(j == 1){
						tempValue++;
					}
					tempStr += tempValue < 10 ? "-0" + tempValue : "-" + tempValue;
				}
				dateArray.push(tempStr);
			}

			for(var i = 0;i < data.length;i++){
				var keyObj = keyObject[data[i]["seqId"]];
				if(keyObj){
					var index = keyObj["nameIndex"];
					var seriesDataTemp = seriesData[index];
					if(!seriesDataTemp){
						seriesData[index] = {};
						seriesData[index]["name"] = keyObj["name"] + "";
						seriesData[index]["type"] = "line";
						seriesData[index]["data"] = [];
						seriesDataTemp = seriesData[index];
					}
					for(var j = keyObj["dataIndex"]; j < dateArray.length;j++){
						if(dateArray[j] == data[i]["days"]){
							seriesDataTemp["data"].push(data[i]["counts"]);
							keyObj["dataIndex"]++;
							break;
						}
						else{
							seriesDataTemp["data"].push(0);
							keyObj["dataIndex"]++;
						}
					}
				}
			}
			
			// 补0
			for(var i in seriesData){
				if(!isNaN(parseInt(i))){
					for(var j = seriesData[i]["data"].length;j < dateArray.length;j++){
						seriesData[i]["data"].push(0);
					}
				}
			}
		}
		
	    return {
	        legendData: legendData,
	        seriesData: seriesData,
	        xData: dateArray
	    };
	}
	
	function genCustomerVoltageLoss(data,keyArray){
		var legendData = []
		var seriesData = [];
		var customerArray = [];
		
		var keyObject = {};
		
		for(var i = 0;i < keyArray.length;i++){
			keyObject[keyArray[i]["seqId"]] = {};
			keyObject[keyArray[i]["seqId"]]["name"] = keyArray[i]["value"];
			keyObject[keyArray[i]["seqId"]]["nameIndex"] = i;
			legendData.push(keyArray[i]["value"] + "");
		}
		
		var lastCustomer;
		var dataIndex = 0;
		if(data[0]){
			lastCustomer = data[0]["customer_number"];
			var keyObj = keyObject[data[0]["voltage_level"]];
			if(keyObj){
				var index = keyObj["nameIndex"];
				seriesData[index] = {};
				seriesData[index]["name"] = keyObj["name"] + "";
				seriesData[index]["type"] = "bar";
				seriesData[index]["data"] = [];
				seriesData[index]["barMaxWidth"] = 20;
				
				seriesData[index]["data"].push(data[0]["counts"]);
				customerArray.push(data[0]["name"]);
			}
		}
		for(var i = 1;i < data.length;i++){
			if(data[i]["customer_number"] != lastCustomer){
				lastCustomer = data[i]["customer_number"];
				customerArray.push(data[i]["name"]);
				dataIndex++;
			}
			
			var keyObj = keyObject[data[i]["voltage_level"]];
			if(keyObj){
				var index = keyObj["nameIndex"];
				if(seriesData[index]){
					seriesData[index]["data"][dataIndex] = data[i]["counts"];
				}
				else{
					seriesData[index] = {};
					seriesData[index]["name"] = keyObj["name"] + "";
					seriesData[index]["type"] = "bar";
					seriesData[index]["barMaxWidth"] = 20;
					seriesData[index]["data"] = [];

					seriesData[index]["data"][dataIndex] = data[i]["counts"];
				}
			}
		}
		
		// 补0
		for(var i in seriesData){
			if(!isNaN(parseInt(i))){
				for(var j = 0;j < customerArray.length;j++){
					if(seriesData[i]["data"]){
						if(!seriesData[i]["data"][j]){
							seriesData[i]["data"][j] = 0;
						}
					}
					else{
						seriesData[i]["data"] = [];
						seriesData[i]["data"][0] = 0;
					}
				}
			}
		}
		
	    return {
	        legendData: legendData,
	        seriesData: seriesData,
	        xData: customerArray
	    };
	}
	
	$(function(){
		$('#startDate').datetimepicker({
			language: 'zh-CN',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			minView: 2,
			forceParse: 0
	    });
		
		$('#endDate').datetimepicker({
			language: 'zh-CN',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			minView: 2,
			forceParse: 0
	    });
		
		var customerPieChart = echarts.init($("#customerPie")[0]);
		var voltageLevelPieChart = echarts.init($("#voltageLevelPie")[0]);
		var repairTypePieChart = echarts.init($("#repairTypePie")[0]);
		var voltageLevelLineChart = echarts.init($("#voltageLevelLine")[0]);
		var repairTypeLineChart = echarts.init($("#repairTypeLine")[0]);
		var customerVoltageLossChart = echarts.init($("#customerVoltageLoss")[0]);
		
		
		customerPieChart.setOption(customerPieOption);
		voltageLevelPieChart.setOption(voltageLevelPieOption);
		repairTypePieChart.setOption(repairTypePieOption);
		voltageLevelLineChart.setOption(voltageLevelLineOption);
		repairTypeLineChart.setOption(repairTypeLineOption);
		customerVoltageLossChart.setOption(customerVoltageLossOption);
	});